<?php
namespace app\wap\controller;

class Article extends Base{
	public $pagesize = 10;
	public function index(){
		$this->is_header       = true;
		$this->is_header_nav   = false;
		$this->add_step('文章');
		$model = model('Article');
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
		}
		// 设置URL参数，用于分页
		$url_params = $where ? $where : array();


		$page = $this->_get('page');
		$page = $page ? $page : 1;
		$url_params['page'] = $page;

		$where['module']    = 'common';
		$where['is_delete'] = 0;
		$where['status']    = 1;
		$order = 'order_id,article_id desc';
		$list  = $model->where($where)->order($order)->paginate(10, false, ['query' => $url_params]);

		$except  = $list->total() / $this->pagesize;
		$total_page = ceil($except);

		if($page >= $total_page){
			$is_hasmore = '';
		}else{
			$is_hasmore = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?page=' . ($page + 1);
		}

		$this->ajax_next_page($list, $page);

		$this->assign('category_list', model('Category')->getCategoryList('article_common'));
		$this->assign('list', $list);
		$this->assign('recommend_list', model('Article')->get_recommend_list('common', 10));
		$this->assign('ajax_next_page', $is_hasmore);

		return $this->view();
	}

	public function detail(){
		$this->is_header       = false;
		$this->is_foot_bartab  = false;
		$this->is_header_nav   = false;
		$article_id = $this->_get('article_id', '对不起，您的操作有误！');
		$info = model('Article')->where(['article_id' => $article_id, 'is_delete' => 0, 'status' => 1])->find();
		if(!$info){
			$this->error('文章不存在！');
		}
		$this->add_step($info['title']);
		$is_fav = model('User')->is_fav($this->_user_id, $article_id, 'article_fav');
		$is_recommend = model('User')->is_fav($this->_user_id, $article_id, 'article_recommend');

		$this->assign('info', $info);
		$this->assign('is_fav', $is_fav);
		$this->assign('is_recommend', $is_recommend);
		$this->assign('recommend_list', model('Article')->get_recommend_list('common', 10));
		return $this->view();
	}

	// 收藏
	public function fav(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$article_id = $this->_get('article_id', '对不起，您的操作有误！');
		if(model('User')->set_fav('Article', 'fav_num', 'article_id', $this->_user_id, $article_id, 'article_fav')){
			$data['node_id'] = 'article-id-' . $article_id;
			$data['node_data']['fav_num'] = model('User')->_total_fav_num;

			$this->success(model('User')->tips_info, '', $data);
		}else{
			$this->error(model('User')->tips_info, '', $data);

		}
	}
	// 收藏
	public function set_recommend(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$article_id = $this->_get('article_id', '对不起，您的操作有误！');
		if(model('User')->set_fav('Article', 'recommend_num', 'article_id', $this->_user_id, $article_id, 'article_recommend')){
			$data['node_id'] = 'article-id-' . $article_id;
			$data['node_data']['recommend_num'] = model('User')->_total_fav_num;

			$this->success('推荐成功！', '', $data);
		}else{
			$this->error(model('User')->tips_info, '', $data);

		}
	}
}